package com.ruoyi.generator.controller.ysd;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.generator.domain.GenForm;
import com.ruoyi.generator.domain.GenTable;
import com.ruoyi.generator.domain.GenTableConfig;
import com.ruoyi.generator.service.IFromCrudBtnsService;
import com.ruoyi.generator.service.IFromCrudTableConfigService;
import com.ruoyi.generator.service.IGenTableService;
import com.ruoyi.generator.service.impl.FromBuildConfigServiceImpl;
import com.ruoyi.system.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * CRUD 菜单
 */
@RestController
public class YsdCrudMenuController extends BaseController
{
    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private IGenTableService genTableService;

    /**
     * 获取路由信息
     */
    @GetMapping("/getMenus")
    public AjaxResult getMenus()
    {
        Long userId = SecurityUtils.getUserId();
        //结果
        Map<String,Object> result=new HashMap<>();
        //获取菜单树信息
        result.put("menus",menuService.selectAllMenuTree());
        //获取需要生成设计表单的信息
        result.put("needGenFrom",genTableService.getNeedGenFromInfo());
        //获取已经配置了菜单的表信息
        result.put("usedTableInfo",genTableService.getUsedTableInfo());
        return AjaxResult.success(result);
    }

    /**
     * 获取系统菜单
     */
    @GetMapping("/getSysMenus")
    public AjaxResult getSysMenus()
    {
        return AjaxResult.success(menuService.selectAllMenuTree());
    }

    /**
     * 根据菜单类型获取对应菜单信息__系统1_业务2
     */
    @GetMapping("/getMenuTree/{type}")
    public AjaxResult selectMenuTreeByType(@PathVariable("type") String type)
    {
        return AjaxResult.success(menuService.selectMenuTreeByType(type));
    }


    /**
     * 保存菜单信息
     * @param menus
     * @return
     */
    @PostMapping("/saveMenusTree")
    public AjaxResult saveMenusTree(@RequestBody List<SysMenu> menus) throws Exception {
        /** 保存菜单树 **/
        AjaxResult result = genTableService.saveMenusTree(menus);
        Map<String,Object> data=new HashMap<>();
        //获取需要生成设计表单的信息
        data.put("needGenFrom",genTableService.getNeedGenFromInfo());
        result.put(AjaxResult.DATA_TAG,data);
        return result;
    }

    /**
     * 保存菜单信息
     * @param menus
     * @return
     */
    @PostMapping("/saveBusinessMenuTree")
    public AjaxResult saveBusinessMenuTree(@RequestBody List<SysMenu> menus) throws Exception {
        /** 获取系统菜单 **/
        List<SysMenu> sysMenus = menuService.selectMenuTreeByType("1");
        menus.addAll(sysMenus);
        /** 保存菜单树 **/
        return genTableService.saveMenusTree(menus);
    }

    /**
     * 更新菜单路由信息
     * 针对在配置菜单时，添加的空菜单，在后期维护对应的访问路由
     * 并将处理好后的路由地址返回，用于前端刷新页面
     * @param menu
     * @return
     */
    @PostMapping("/updateMenusPath")
    public AjaxResult updateMenusPath(@RequestBody SysMenu menu) throws Exception {
        /** 保存菜单树 **/
        return genTableService.updateMenusPath(menu);
    }

    /**
     * 删除菜单
     * @param menuId
     * @return
     */
    @GetMapping("/menu/del/{menuId}")
    public AjaxResult delMenu(@PathVariable("menuId") Long menuId) throws Exception {
        /** 保存菜单树 **/
        return genTableService.delMenu(menuId);
    }

    /**
     * 根据表名获取对应的配置菜单的信息
     * @param bindTable
     * @returnT
     */
    @GetMapping("/menu/bindInfo/{bindTable}")
    public AjaxResult menuBindTableInfo(@PathVariable("bindTable") String bindTable) throws Exception {
        if(StringUtils.isEmptyIncludeNull(bindTable)){
            return AjaxResult.success();
        }
        return AjaxResult.success(menuService.menuBindTableInfo(bindTable));
    }

    /**
     * 根据菜单ID获取对应菜单权限信息
     * @returnT
     */
    @GetMapping("/menu/perms/{menuId}")
    public AjaxResult menuPermsInfo(@PathVariable("menuId") String menuId) throws Exception {
        if(StringUtils.isEmptyIncludeNull(menuId)){
            return AjaxResult.success();
        }
        return AjaxResult.success(menuService.menuPermsInfo(menuId));
    }

}
